package org.association.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.association.entity.UserEntity;
import org.association.service.TradeService;
import org.association.service.UserService;
import org.association.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@Api(tags = "交易接口")
@RestController
@RequestMapping("/trade")
@CrossOrigin
public class TradeController {

    @Autowired
    private UserService userService;

    @Autowired
    private TradeService tradeService;

    @ApiOperation(value = "查询账户")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "address", value = "待查询账户地址", dataTypeClass = String.class, required = true),
    })
    @PostMapping("/asset")
    public R queryBalance(@RequestParam String address) {

        if(StringUtils.isEmpty(address)) {
            return R.error().message("查询账户为空！");
        }

        int balance = tradeService.getMemberAssetWBA(address);
        if(balance == -1){
            return R.error().message("发生其他错误！");
        }
        else{
            return R.ok().put("result", balance);
        }

    }

    @ApiOperation(value = "转账")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "fromAddress", value = "发起者账号", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "toAddress", value = "接收者账号", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "amount", value = "交易数额", dataTypeClass = Integer.class, required = true)
    })
    @PostMapping("/transfer")
    public R transfer(@RequestParam String fromAddress, @RequestParam String toAddress, @RequestParam Integer amount) {

        if(amount <= 0){
            return R.error().message("转账金额为正数！");
        }

        if(Integer.parseInt(queryBalance(fromAddress).getData().get("result").toString()) < amount){
            return R.error().message("余额不足，无法转账！");
        }

        if(StringUtils.isEmpty(fromAddress)) {
            return R.error().message("发起者账号为空！");
        }
        else if(StringUtils.isEmpty(toAddress)) {
            return R.error().message("接收者账号为空！");
        }

        Boolean result = tradeService.transferWBA(fromAddress, toAddress, amount);

        if(result){
            return R.ok().put("result", "转账成功！");
        }
        else{
            return R.error().message("转账失败或发生其他错误！");
        }
    }


    @ApiOperation(value = "充值")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "address", value = "充值者账号", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "amount", value = "充值数额", dataTypeClass = Integer.class, required = true)
    })
    @PostMapping("/inject")
    public R inject(@RequestParam String address, @RequestParam Integer amount) {

        if(amount <= 0){
            return R.error().message("充值金额为正数！");
        }

        if(StringUtils.isEmpty(address)) {
            return R.error().message("充值账号为空！");
        }


        Boolean result = tradeService.injectAssetWBA(address, amount);
        if(result){
            return R.ok().put("result", "充值成功！");
        }
        else{
            // 其他情况
            return R.error().message("发生其他错误！");
        }
    }

    @ApiOperation(value = "提现")
    @ApiImplicitParams( value = {
            @ApiImplicitParam(name = "address", value = "提现者账号", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "amount", value = "提现数额", dataTypeClass = Integer.class, required = true)
    })
    @PostMapping("/withdraw")
    public R withdraw(@RequestParam String address, @RequestParam Integer amount) {

        if(StringUtils.isEmpty(address)) {
            return R.error().message("提现账号为空！");
        }

        Boolean result = tradeService.withdrawAssetWBA(address, amount);
        if(result){
            return R.ok().put("result", "提现成功！");
        }
        else{
            // 其他情况
            return R.error().message("发生其他错误！");
        }
    }
}
